SlideShare a Scribd company logo
Class No.23  Data Structures http://ecomputernotes.com
Expression Tree The inner nodes contain operators while leaf nodes contain operands. a c + b g * + + d * * e f http://ecomputernotes.com
Expression Tree The tree is binary because the operators are binary. a c + b g * + + d * * e f http://ecomputernotes.com
Expression Tree This is not necessary. A unary operator (!, e.g.) will have only one subtree. a c + b g * + + d * * e f http://ecomputernotes.com
Expression Tree Inorder traversal yields: a+b*c+d*e+f*g a c + b g * + + d * * e f http://ecomputernotes.com
Enforcing Parenthesis void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL ){ cout << &quot;(&quot;; inorder(treeNode->getLeft()); cout << &quot;)&quot;; cout << *(treeNode->getInfo()); cout << &quot;(&quot;; inorder(treeNode->getRight()); cout << &quot;)&quot;; } } http://ecomputernotes.com
Expression Tree Inorder : (a+(b*c))+(((d*e)+f)*g) a c + b g * + + d * * e f http://ecomputernotes.com
Expression Tree Postorder traversal: a b c * + d e * f + g * + which is the postfix form. a c + b g * + + d * * e f http://ecomputernotes.com
Constructing Expression Tree Algorithm to convert postfix expression into an expression tree. We already have an expression to convert an infix expression to postfix. Read a symbol from the postfix expression. If symbol is an operand, put it in a one node tree and push it on a stack. If symbol is an operator, pop two trees from the stack, form a new tree with operator as the root and T 1  and T 2  as left and right subtrees and push this tree on the stack. http://ecomputernotes.com
Constructing Expression Tree a b + c d e + * * stack http://ecomputernotes.com
Constructing Expression Tree a b  + c d e + * * b a Stack is growing left to right If symbol is an operand, put it in a one node tree and push it on a stack. top http://ecomputernotes.com
Constructing Expression Tree a b +  c d e + * * b a Stack is growing left to right + If symbol is an operator, pop two trees from the stack, form a new tree with operator as the root and T 1  and T 2  as left and right subtrees and push this tree on the stack. http://ecomputernotes.com
Constructing Expression Tree a b +   c d e  + * * b a + d c e http://ecomputernotes.com
Constructing Expression Tree a b +   c d e   +  * * b a + c e d + http://ecomputernotes.com
Constructing Expression Tree a b +   c d e   +   *  * b a + c e d + * http://ecomputernotes.com
Constructing Expression Tree a b +   c d e   +   *   * b a + c e d + * * http://ecomputernotes.com
Other Uses of Binary Trees Huffman Encoding http://ecomputernotes.com
Huffman Encoding Data compression plays a significant role in computer networks. To transmit data to its destination faster, it is necessary to either increase the data rate of the transmission media or to simply send less data.  Improvements with regard to the transmission media has led to increase in the rate. The other options is to send less data by means of data compression. Compression methods are used for text, images, voice and other types of data (space probes). http://ecomputernotes.com
Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths for the letters used in the original message.  Huffman code is also part of the JPEG image compression scheme. The algorithm was introduced by David Huffman in 1952 as part of a course assignment at MIT. http://ecomputernotes.com http://ecomputernotes.com
Huffman Encoding To understand Huffman encoding, it is best to use a simple example.  Encoding the 32-character phrase: &quot; traversing threaded binary trees &quot;,  If we send the phrase as a message in a network using standard 8-bit ASCII codes, we would have to send 8*32= 256 bits. Using the Huffman algorithm, we can send the message with only 116 bits.
Huffman Encoding List all the letters used, including the &quot;space&quot; character, along with the frequency with which they occur in the message.  Consider each of these (character,frequency) pairs to be nodes; they are actually leaf nodes, as we will see.  Pick the two nodes with the lowest frequency, and if there is a tie, pick randomly amongst those with equal frequencies.
Huffman Encoding Make a new node out of these two, and make the two nodes its children.  This new node is assigned the sum of the frequencies of its children.  Continue the process of combining the two nodes of lowest frequency until only one node, the root, remains.
Huffman Encoding Original text:  traversing threaded binary trees size: 33 characters (space and newline) NL : 1 SP : 3 a : 3 b : 1 d : 2 e : 5 g : 1 h : 1 i : 2 n : 2 r : 5 s : 2 t : 3 v : 1 y : 1
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 is equal to sum  of the frequencies of  the two children nodes.
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 There a number of ways to combine nodes. We have chosen just one such way.
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 4 4
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 6
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 9 10
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10
Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10 33

More Related Content

DOCX
15 practical grep command examples in linux
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 37
PPT
computer notes - Data Structures - 36
PPT
computer notes - Data Structures - 32
PPT
computer notes - Data Structures - 38
15 practical grep command examples in linux
computer notes - Data Structures - 19
computer notes - Data Structures - 6
computer notes - Data Structures - 2
computer notes - Data Structures - 37
computer notes - Data Structures - 36
computer notes - Data Structures - 32
computer notes - Data Structures - 38

Viewers also liked (20)

PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 24
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 33
PPT
computer notes - Data Structures - 34
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 27
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 25
PPT
computer notes - Data Structures - 7
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 39
computer notes - Data Structures - 35
computer notes - Data Structures - 9
computer notes - Data Structures - 8
computer notes - Data Structures - 18
computer notes - Data Structures - 1
computer notes - Data Structures - 16
computer notes - Data Structures - 24
computer notes - Deleting a node
computer notes - Data Structures - 33
computer notes - Data Structures - 34
computer notes - Data Structures - 31
computer notes - Data Structures - 28
computer notes - Data Structures - 27
computer notes - Data Structures - 12
computer notes - Data Structures - 20
computer notes - Data Structures - 25
computer notes - Data Structures - 7
computer notes - Data Structures - 4
computer notes - Data Structures - 14
computer notes - Data Structures - 39
Ad

Similar to computer notes - Data Structures - 23 (20)

PPT
Computer notes - Expression Tree
PDF
LEC 7-DS ALGO(expression and huffman).pdf
PPTX
complier design unit 4 for helping students
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 13
PPT
Huffman coding presentation Sukkur iba.ppt
PDF
Ch06
PDF
Binary Trees
DOC
Assignments of source coding theory and applications
PDF
3b. LMD & RMD.pdf
PPT
Top_down_Parsing_ full_detail_explanation
PDF
TheUnixPipeandFilters
PDF
TheUnixPipeandFilters
PPT
Computer notes - Sorting
PDF
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
PDF
Common mistakes in C programming
PDF
Introduction to Compiler Development
PPT
Hufman coding basic
PDF
j001adcpresentation-2112170415 23.pdf
PPTX
Huffman Algorithm and its Application by Ekansh Agarwal
Computer notes - Expression Tree
LEC 7-DS ALGO(expression and huffman).pdf
complier design unit 4 for helping students
computer notes - Data Structures - 11
computer notes - Data Structures - 13
Huffman coding presentation Sukkur iba.ppt
Ch06
Binary Trees
Assignments of source coding theory and applications
3b. LMD & RMD.pdf
Top_down_Parsing_ full_detail_explanation
TheUnixPipeandFilters
TheUnixPipeandFilters
Computer notes - Sorting
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
Common mistakes in C programming
Introduction to Compiler Development
Hufman coding basic
j001adcpresentation-2112170415 23.pdf
Huffman Algorithm and its Application by Ekansh Agarwal
Ad

More from ecomputernotes (17)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 22
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 5
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
PPT
computer notes - Data Structures - 29
computer notes - Data Structures - 30
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 22
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 10
computer notes - Data Structures - 5
Computer notes - Controlling User Access
Computer notes - Using SET Operator
computer notes - Data Structures - 29

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
Encapsulation theory and applications.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PPT
Teaching material agriculture food technology
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
Spectroscopy.pptx food analysis technology
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Cloud computing and distributed systems.
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Machine learning based COVID-19 study performance prediction
Encapsulation theory and applications.pdf
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Teaching material agriculture food technology
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
sap open course for s4hana steps from ECC to s4
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx
Spectral efficient network and resource selection model in 5G networks
Spectroscopy.pptx food analysis technology
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Cloud computing and distributed systems.
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Unlocking AI with Model Context Protocol (MCP)
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

computer notes - Data Structures - 23

  • 1. Class No.23 Data Structures http://ecomputernotes.com
  • 2. Expression Tree The inner nodes contain operators while leaf nodes contain operands. a c + b g * + + d * * e f http://ecomputernotes.com
  • 3. Expression Tree The tree is binary because the operators are binary. a c + b g * + + d * * e f http://ecomputernotes.com
  • 4. Expression Tree This is not necessary. A unary operator (!, e.g.) will have only one subtree. a c + b g * + + d * * e f http://ecomputernotes.com
  • 5. Expression Tree Inorder traversal yields: a+b*c+d*e+f*g a c + b g * + + d * * e f http://ecomputernotes.com
  • 6. Enforcing Parenthesis void inorder(TreeNode<int>* treeNode) { if( treeNode != NULL ){ cout << &quot;(&quot;; inorder(treeNode->getLeft()); cout << &quot;)&quot;; cout << *(treeNode->getInfo()); cout << &quot;(&quot;; inorder(treeNode->getRight()); cout << &quot;)&quot;; } } http://ecomputernotes.com
  • 7. Expression Tree Inorder : (a+(b*c))+(((d*e)+f)*g) a c + b g * + + d * * e f http://ecomputernotes.com
  • 8. Expression Tree Postorder traversal: a b c * + d e * f + g * + which is the postfix form. a c + b g * + + d * * e f http://ecomputernotes.com
  • 9. Constructing Expression Tree Algorithm to convert postfix expression into an expression tree. We already have an expression to convert an infix expression to postfix. Read a symbol from the postfix expression. If symbol is an operand, put it in a one node tree and push it on a stack. If symbol is an operator, pop two trees from the stack, form a new tree with operator as the root and T 1 and T 2 as left and right subtrees and push this tree on the stack. http://ecomputernotes.com
  • 10. Constructing Expression Tree a b + c d e + * * stack http://ecomputernotes.com
  • 11. Constructing Expression Tree a b + c d e + * * b a Stack is growing left to right If symbol is an operand, put it in a one node tree and push it on a stack. top http://ecomputernotes.com
  • 12. Constructing Expression Tree a b + c d e + * * b a Stack is growing left to right + If symbol is an operator, pop two trees from the stack, form a new tree with operator as the root and T 1 and T 2 as left and right subtrees and push this tree on the stack. http://ecomputernotes.com
  • 13. Constructing Expression Tree a b + c d e + * * b a + d c e http://ecomputernotes.com
  • 14. Constructing Expression Tree a b + c d e + * * b a + c e d + http://ecomputernotes.com
  • 15. Constructing Expression Tree a b + c d e + * * b a + c e d + * http://ecomputernotes.com
  • 16. Constructing Expression Tree a b + c d e + * * b a + c e d + * * http://ecomputernotes.com
  • 17. Other Uses of Binary Trees Huffman Encoding http://ecomputernotes.com
  • 18. Huffman Encoding Data compression plays a significant role in computer networks. To transmit data to its destination faster, it is necessary to either increase the data rate of the transmission media or to simply send less data. Improvements with regard to the transmission media has led to increase in the rate. The other options is to send less data by means of data compression. Compression methods are used for text, images, voice and other types of data (space probes). http://ecomputernotes.com
  • 19. Huffman Encoding Huffman code is method for the compression for standard text documents. It makes use of a binary tree to develop codes of varying lengths for the letters used in the original message. Huffman code is also part of the JPEG image compression scheme. The algorithm was introduced by David Huffman in 1952 as part of a course assignment at MIT. http://ecomputernotes.com http://ecomputernotes.com
  • 20. Huffman Encoding To understand Huffman encoding, it is best to use a simple example. Encoding the 32-character phrase: &quot; traversing threaded binary trees &quot;, If we send the phrase as a message in a network using standard 8-bit ASCII codes, we would have to send 8*32= 256 bits. Using the Huffman algorithm, we can send the message with only 116 bits.
  • 21. Huffman Encoding List all the letters used, including the &quot;space&quot; character, along with the frequency with which they occur in the message. Consider each of these (character,frequency) pairs to be nodes; they are actually leaf nodes, as we will see. Pick the two nodes with the lowest frequency, and if there is a tie, pick randomly amongst those with equal frequencies.
  • 22. Huffman Encoding Make a new node out of these two, and make the two nodes its children. This new node is assigned the sum of the frequencies of its children. Continue the process of combining the two nodes of lowest frequency until only one node, the root, remains.
  • 23. Huffman Encoding Original text: traversing threaded binary trees size: 33 characters (space and newline) NL : 1 SP : 3 a : 3 b : 1 d : 2 e : 5 g : 1 h : 1 i : 2 n : 2 r : 5 s : 2 t : 3 v : 1 y : 1
  • 24. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 is equal to sum of the frequencies of the two children nodes.
  • 25. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 There a number of ways to combine nodes. We have chosen just one such way.
  • 26. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2
  • 27. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 4 4
  • 28. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 6
  • 29. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 9 10
  • 30. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10
  • 31. Huffman Encoding v 1 y 1 SP 3 r 5 h 1 e 5 g 1 b 1 NL 1 s 2 n 2 i 2 d 2 t 3 a 3 2 2 2 5 4 4 4 8 6 14 9 19 10 33

Editor's Notes

  • #3: Start of lecture 25.
  • #22: Start of Lecture 26
  • #32: End of lecture 25.